package com.jxw.system.mediaserver;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.Map;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import org.apache.http.protocol.HttpRequestHandlerRegistry;
import org.apache.http.protocol.HttpService;

/* loaded from: classes.dex */
public class HttpFileServer {
    private static int ID = 0;
    private static final String TAG = "HttpFileServer";
    private HttpContext mHttpContext;
    private HttpService mHttpService;
    private int mPort = 8100;
    private boolean mRunning = true;
    private ServerSocket mServerSocket;
    private Runnable mServerTask;
    private Thread mServerThread;
    private Handler mWorkHandler;
    private HandlerThread mWorkThread;

    static /* synthetic */ int access$008() {
        int i = ID;
        ID = i + 1;
        return i;
    }

    /* loaded from: classes.dex */
    private final class HttpThread extends Thread {
        private HttpContext mContext;
        private int mId;
        private HttpService mService;
        private Socket mSocket;

        public HttpThread(HttpContext httpContext, HttpService httpService, Socket socket) {
            this.mContext = httpContext;
            this.mService = httpService;
            this.mSocket = socket;
            synchronized (HttpFileServer.class) {
                this.mId = HttpFileServer.access$008();
            }
        }

        /* JADX WARN: Removed duplicated region for block: B:41:0x0080 A[EXC_TOP_SPLITTER, SYNTHETIC] */
        @Override // java.lang.Thread, java.lang.Runnable
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct code enable 'Show inconsistent code' option in preferences
        */
        public void run() {
            /*
                r5 = this;
                java.lang.String r0 = "HttpFileServer"
                r1 = 0
                org.apache.http.impl.DefaultHttpServerConnection r2 = new org.apache.http.impl.DefaultHttpServerConnection     // Catch: java.lang.Throwable -> L5d org.apache.http.HttpException -> L6a java.io.IOException -> L71
                r2.<init>()     // Catch: java.lang.Throwable -> L5d org.apache.http.HttpException -> L6a java.io.IOException -> L71
                org.apache.http.params.BasicHttpParams r1 = new org.apache.http.params.BasicHttpParams     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                r1.<init>()     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                java.lang.String r3 = "http.protocol.version"
                org.apache.http.HttpVersion r4 = org.apache.http.HttpVersion.HTTP_1_1     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                r1.setParameter(r3, r4)     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                java.lang.String r3 = "http.tcp.nodelay"
                r4 = 1
                r1.setBooleanParameter(r3, r4)     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                java.lang.String r3 = "http.socket.timeout"
                r4 = 600000(0x927c0, float:8.40779E-40)
                r1.setIntParameter(r3, r4)     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                java.lang.String r3 = "http.socket.linger"
                r1.setIntParameter(r3, r4)     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                java.net.Socket r1 = r5.mSocket     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                r1.setSoTimeout(r4)     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                java.net.Socket r1 = r5.mSocket     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                org.apache.http.params.BasicHttpParams r3 = new org.apache.http.params.BasicHttpParams     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                r3.<init>()     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                r2.bind(r1, r3)     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                r1.<init>()     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                java.lang.String r3 = "accept an connection, process it, thread: "
                r1.append(r3)     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                int r3 = r5.mId     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                r1.append(r3)     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                java.lang.String r1 = r1.toString()     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                android.util.Log.e(r0, r1)     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                org.apache.http.protocol.HttpService r1 = r5.mService     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                org.apache.http.protocol.HttpContext r3 = r5.mContext     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                r1.handleRequest(r2, r3)     // Catch: java.lang.Throwable -> L57 org.apache.http.HttpException -> L59 java.io.IOException -> L5b
                r2.close()     // Catch: java.io.IOException -> L78
                goto L7c
            L57:
                r0 = move-exception
                goto L5f
            L59:
                goto L6b
            L5b:
                goto L72
            L5d:
                r0 = move-exception
                r2 = r1
            L5f:
                if (r2 == 0) goto L69
                r2.close()     // Catch: java.io.IOException -> L65
                goto L69
            L65:
                r1 = move-exception
                r1.printStackTrace()
            L69:
                throw r0
            L6a:
                r2 = r1
            L6b:
                if (r2 == 0) goto L7c
                r2.close()     // Catch: java.io.IOException -> L78
                goto L7c
            L71:
                r2 = r1
            L72:
                if (r2 == 0) goto L7c
                r2.close()     // Catch: java.io.IOException -> L78
                goto L7c
            L78:
                r1 = move-exception
                r1.printStackTrace()
            L7c:
                java.net.Socket r1 = r5.mSocket
                if (r1 == 0) goto L88
                r1.close()     // Catch: java.io.IOException -> L84
                goto L88
            L84:
                r1 = move-exception
                r1.printStackTrace()
            L88:
                java.lang.StringBuilder r1 = new java.lang.StringBuilder
                r1.<init>()
                java.lang.String r2 = "accept an connection, process complete, thread: "
                r1.append(r2)
                int r2 = r5.mId
                r1.append(r2)
                java.lang.String r1 = r1.toString()
                android.util.Log.e(r0, r1)
                return
            */
            throw new UnsupportedOperationException("Method not decompiled: com.jxw.system.mediaserver.HttpFileServer.HttpThread.run():void");
        }
    }

    public static HttpFileServer create(int i, Map<String, HttpRequestHandler> map) {
        HttpFileServer httpFileServer = new HttpFileServer();
        try {
            httpFileServer.setup(i, map);
            return httpFileServer;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void exit() {
        this.mRunning = false;
        this.mServerThread.interrupt();
        try {
            this.mServerThread.join(10000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void setup(int i, Map<String, HttpRequestHandler> map) throws Exception {
        if (i != -1) {
            this.mPort = i;
        }
        this.mWorkThread = new HandlerThread("httpserver");
        this.mWorkThread.start();
        this.mWorkHandler = new Handler(this.mWorkThread.getLooper()) { // from class: com.jxw.system.mediaserver.HttpFileServer.1
            @Override // android.os.Handler
            public void handleMessage(Message message) {
                int i2 = message.what;
            }
        };
        this.mServerTask = new Runnable() { // from class: com.jxw.system.mediaserver.HttpFileServer.2
            @Override // java.lang.Runnable
            public void run() {
                try {
                    HttpFileServer.this.mServerSocket = new ServerSocket();
                    HttpFileServer.this.mServerSocket.setReuseAddress(true);
                    HttpFileServer.this.mServerSocket.bind(new InetSocketAddress(HttpFileServer.this.mPort));
                    while (HttpFileServer.this.mRunning) {
                        try {
                            Socket accept = HttpFileServer.this.mServerSocket.accept();
                            accept.setKeepAlive(true);
                            accept.setSoTimeout(600000);
                            new HttpThread(HttpFileServer.this.mHttpContext, HttpFileServer.this.mHttpService, accept).run();
                        } catch (SocketTimeoutException e) {
                            Log.e(HttpFileServer.TAG, "======SocketTimeoutException=======");
                            e.printStackTrace();
                        }
                    }
                    HttpFileServer.this.mServerSocket.close();
                } catch (SocketException e2) {
                    Log.e(HttpFileServer.TAG, "=========SocketException==========");
                    e2.printStackTrace();
                } catch (IOException e3) {
                    Log.e(HttpFileServer.TAG, "=========IOException==========");
                    e3.printStackTrace();
                }
                Log.e(HttpFileServer.TAG, "http server loop exited!");
                HttpFileServer.this.mRunning = false;
            }
        };
        this.mHttpContext = new BasicHttpContext();
        this.mHttpService = new HttpService(new BasicHttpProcessor(), new DefaultConnectionReuseStrategy(), new DefaultHttpResponseFactory());
        HttpRequestHandlerRegistry httpRequestHandlerRegistry = new HttpRequestHandlerRegistry();
        for (Map.Entry<String, HttpRequestHandler> entry : map.entrySet()) {
            httpRequestHandlerRegistry.register(entry.getKey(), entry.getValue());
        }
        this.mHttpService.setHandlerResolver(httpRequestHandlerRegistry);
        this.mServerThread = new Thread(this.mServerTask);
        this.mServerThread.start();
    }
}
